home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / audioop.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  35.7 KB  |  1,394 lines

  1. /* audioopmodule - Module to detect peak values in arrays */
  2.  
  3. #include "Python.h"
  4.  
  5. #if SIZEOF_INT == 4
  6. typedef int Py_Int32;
  7. typedef unsigned int Py_UInt32;
  8. #else
  9. #if SIZEOF_LONG == 4
  10. typedef long Py_Int32;
  11. typedef unsigned long Py_UInt32;
  12. #else
  13. #error "No 4-byte integral type"
  14. #endif
  15. #endif
  16.  
  17. #if defined(__CHAR_UNSIGNED__)
  18. #if defined(signed)
  19. !ERROR!; READ THE SOURCE FILE!;
  20. /* This module currently does not work on systems where only unsigned
  21.    characters are available.  Take it out of Setup.  Sorry. */
  22. #endif
  23. #endif
  24.  
  25. #include "mymath.h"
  26.  
  27. /* Code shamelessly stolen from sox,
  28. ** (c) Craig Reese, Joe Campbell and Jeff Poskanzer 1989 */
  29.  
  30. #define MINLIN -32768
  31. #define MAXLIN 32767
  32. #define LINCLIP(x) do { if ( x < MINLIN ) x = MINLIN ; \
  33.                         else if ( x > MAXLIN ) x = MAXLIN; \
  34.                       } while ( 0 )
  35.  
  36. static unsigned char st_linear_to_ulaw( /* int sample */ );
  37.  
  38. /*
  39. ** This macro converts from ulaw to 16 bit linear, faster.
  40. **
  41. ** Jef Poskanzer
  42. ** 23 October 1989
  43. **
  44. ** Input: 8 bit ulaw sample
  45. ** Output: signed 16 bit linear sample
  46. */
  47. #define st_ulaw_to_linear(ulawbyte) ulaw_table[ulawbyte]
  48.  
  49. static int ulaw_table[256] = {
  50.     -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
  51.     -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
  52.     -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
  53.     -11900, -11388, -10876, -10364,  -9852,  -9340,  -8828,  -8316,
  54.     -7932,  -7676,  -7420,  -7164,  -6908,  -6652,  -6396,  -6140,
  55.     -5884,  -5628,  -5372,  -5116,  -4860,  -4604,  -4348,  -4092,
  56.     -3900,  -3772,  -3644,  -3516,  -3388,  -3260,  -3132,  -3004,
  57.     -2876,  -2748,  -2620,  -2492,  -2364,  -2236,  -2108,  -1980,
  58.     -1884,  -1820,  -1756,  -1692,  -1628,  -1564,  -1500,  -1436,
  59.     -1372,  -1308,  -1244,  -1180,  -1116,  -1052,   -988,   -924,
  60.     -876,   -844,   -812,   -780,   -748,   -716,   -684,   -652,
  61.     -620,   -588,   -556,   -524,   -492,   -460,   -428,   -396,
  62.     -372,   -356,   -340,   -324,   -308,   -292,   -276,   -260,
  63.     -244,   -228,   -212,   -196,   -180,   -164,   -148,   -132,
  64.     -120,   -112,   -104,    -96,    -88,    -80,    -72,    -64,
  65.     -56,    -48,    -40,    -32,    -24,    -16,     -8,      0,
  66.     32124,  31100,  30076,  29052,  28028,  27004,  25980,  24956,
  67.     23932,  22908,  21884,  20860,  19836,  18812,  17788,  16764,
  68.     15996,  15484,  14972,  14460,  13948,  13436,  12924,  12412,
  69.     11900,  11388,  10876,  10364,   9852,   9340,   8828,   8316,
  70.     7932,   7676,   7420,   7164,   6908,   6652,   6396,   6140,
  71.     5884,   5628,   5372,   5116,   4860,   4604,   4348,   4092,
  72.     3900,   3772,   3644,   3516,   3388,   3260,   3132,   3004,
  73.     2876,   2748,   2620,   2492,   2364,   2236,   2108,   1980,
  74.     1884,   1820,   1756,   1692,   1628,   1564,   1500,   1436,
  75.     1372,   1308,   1244,   1180,   1116,   1052,    988,    924,
  76.     876,    844,    812,    780,    748,    716,    684,    652,
  77.     620,    588,    556,    524,    492,    460,    428,    396,
  78.     372,    356,    340,    324,    308,    292,    276,    260,
  79.     244,    228,    212,    196,    180,    164,    148,    132,
  80.     120,    112,    104,     96,     88,     80,     72,     64,
  81.     56,     48,     40,     32,     24,     16,      8,      0 };
  82.  
  83. /* #define ZEROTRAP    /* turn on the trap as per the MIL-STD */
  84. #define BIAS 0x84   /* define the add-in bias for 16 bit samples */
  85. #define CLIP 32635
  86.  
  87. static unsigned char
  88. st_linear_to_ulaw( sample )
  89.     int sample;
  90. {
  91.     static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
  92.                    4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  93.                    5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  94.                    5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  95.                    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  96.                    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  97.                    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  98.                    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  99.                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  100.                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  101.                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  102.                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  103.                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  104.                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  105.                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  106.                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
  107.     int sign, exponent, mantissa;
  108.     unsigned char ulawbyte;
  109.  
  110.     /* Get the sample into sign-magnitude. */
  111.     sign = (sample >> 8) & 0x80;        /* set aside the sign */
  112.     if ( sign != 0 ) sample = -sample;    /* get magnitude */
  113.     if ( sample > CLIP ) sample = CLIP;    /* clip the magnitude */
  114.  
  115.     /* Convert from 16 bit linear to ulaw. */
  116.     sample = sample + BIAS;
  117.     exponent = exp_lut[( sample >> 7 ) & 0xFF];
  118.     mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
  119.     ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
  120. #ifdef ZEROTRAP
  121.     if ( ulawbyte == 0 ) ulawbyte = 0x02;    /* optional CCITT trap */
  122. #endif
  123.  
  124.     return ulawbyte;
  125. }
  126. /* End of code taken from sox */
  127.  
  128. /* Intel ADPCM step variation table */
  129. static int indexTable[16] = {
  130.     -1, -1, -1, -1, 2, 4, 6, 8,
  131.     -1, -1, -1, -1, 2, 4, 6, 8,
  132. };
  133.  
  134. static int stepsizeTable[89] = {
  135.     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  136.     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  137.     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  138.     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  139.     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  140.     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  141.     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  142.     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  143.     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  144. };
  145.     
  146. #define CHARP(cp, i) ((signed char *)(cp+i))
  147. #define SHORTP(cp, i) ((short *)(cp+i))
  148. #define LONGP(cp, i) ((Py_Int32 *)(cp+i))
  149.  
  150.  
  151.  
  152. static PyObject *AudioopError;
  153.  
  154. static PyObject *
  155. audioop_getsample(self, args)
  156.     PyObject *self;
  157.         PyObject *args;
  158. {
  159.     signed char *cp;
  160.     int len, size, val = 0;
  161.     int i;
  162.  
  163.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &size, &i) )
  164.         return 0;
  165.     if ( size != 1 && size != 2 && size != 4 ) {
  166.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  167.         return 0;
  168.     }
  169.     if ( i < 0 || i >= len/size ) {
  170.         PyErr_SetString(AudioopError, "Index out of range");
  171.         return 0;
  172.     }
  173.     if ( size == 1 )      val = (int)*CHARP(cp, i);
  174.     else if ( size == 2 ) val = (int)*SHORTP(cp, i*2);
  175.     else if ( size == 4 ) val = (int)*LONGP(cp, i*4);
  176.     return PyInt_FromLong(val);
  177. }
  178.  
  179. static PyObject *
  180. audioop_max(self, args)
  181.     PyObject *self;
  182.         PyObject *args;
  183. {
  184.     signed char *cp;
  185.     int len, size, val = 0;
  186.     int i;
  187.     int max = 0;
  188.  
  189.     if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) )
  190.         return 0;
  191.     if ( size != 1 && size != 2 && size != 4 ) {
  192.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  193.         return 0;
  194.     }
  195.     for ( i=0; i<len; i+= size) {
  196.         if ( size == 1 )      val = (int)*CHARP(cp, i);
  197.         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  198.         else if ( size == 4 ) val = (int)*LONGP(cp, i);
  199.         if ( val < 0 ) val = (-val);
  200.         if ( val > max ) max = val;
  201.     }
  202.     return PyInt_FromLong(max);
  203. }
  204.  
  205. static PyObject *
  206. audioop_minmax(self, args)
  207.     PyObject *self;
  208.         PyObject *args;
  209. {
  210.     signed char *cp;
  211.     int len, size, val = 0;
  212.     int i;
  213.     int min = 0x7fffffff, max = -0x7fffffff;
  214.  
  215.     if (!PyArg_Parse(args, "(s#i)", &cp, &len, &size))
  216.         return NULL;
  217.     if (size != 1 && size != 2 && size != 4) {
  218.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  219.         return NULL;
  220.     }
  221.     for (i = 0; i < len; i += size) {
  222.         if (size == 1) val = (int) *CHARP(cp, i);
  223.         else if (size == 2) val = (int) *SHORTP(cp, i);
  224.         else if (size == 4) val = (int) *LONGP(cp, i);
  225.         if (val > max) max = val;
  226.         if (val < min) min = val;
  227.     }
  228.     return Py_BuildValue("(ii)", min, max);
  229. }
  230.  
  231. static PyObject *
  232. audioop_avg(self, args)
  233.     PyObject *self;
  234.         PyObject *args;
  235. {
  236.     signed char *cp;
  237.     int len, size, val = 0;
  238.     int i;
  239.     double avg = 0.0;
  240.  
  241.     if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) )
  242.         return 0;
  243.     if ( size != 1 && size != 2 && size != 4 ) {
  244.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  245.         return 0;
  246.     }
  247.     for ( i=0; i<len; i+= size) {
  248.         if ( size == 1 )      val = (int)*CHARP(cp, i);
  249.         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  250.         else if ( size == 4 ) val = (int)*LONGP(cp, i);
  251.         avg += val;
  252.     }
  253.     if ( len == 0 )
  254.         val = 0;
  255.     else
  256.         val = (int)(avg / (double)(len/size));
  257.     return PyInt_FromLong(val);
  258. }
  259.  
  260. static PyObject *
  261. audioop_rms(self, args)
  262.     PyObject *self;
  263.         PyObject *args;
  264. {
  265.     signed char *cp;
  266.     int len, size, val = 0;
  267.     int i;
  268.     double sum_squares = 0.0;
  269.  
  270.     if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) )
  271.         return 0;
  272.     if ( size != 1 && size != 2 && size != 4 ) {
  273.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  274.         return 0;
  275.     }
  276.     for ( i=0; i<len; i+= size) {
  277.         if ( size == 1 )      val = (int)*CHARP(cp, i);
  278.         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  279.         else if ( size == 4 ) val = (int)*LONGP(cp, i);
  280.         sum_squares += (double)val*(double)val;
  281.     }
  282.     if ( len == 0 )
  283.         val = 0;
  284.     else
  285.         val = (int)sqrt(sum_squares / (double)(len/size));
  286.     return PyInt_FromLong(val);
  287. }
  288.  
  289. static double _sum2(a, b, len)
  290.     short *a;
  291.         short *b;
  292.         int len;
  293. {
  294.     int i;
  295.     double sum = 0.0;
  296.  
  297.     for( i=0; i<len; i++) {
  298.         sum = sum + (double)a[i]*(double)b[i];
  299.     }
  300.     return sum;
  301. }
  302.  
  303. /*
  304. ** Findfit tries to locate a sample within another sample. Its main use
  305. ** is in echo-cancellation (to find the feedback of the output signal in
  306. ** the input signal).
  307. ** The method used is as follows:
  308. **
  309. ** let R be the reference signal (length n) and A the input signal (length N)
  310. ** with N > n, and let all sums be over i from 0 to n-1.
  311. **
  312. ** Now, for each j in {0..N-n} we compute a factor fj so that -fj*R matches A
  313. ** as good as possible, i.e. sum( (A[j+i]+fj*R[i])^2 ) is minimal. This
  314. ** equation gives fj = sum( A[j+i]R[i] ) / sum(R[i]^2).
  315. **
  316. ** Next, we compute the relative distance between the original signal and
  317. ** the modified signal and minimize that over j:
  318. ** vj = sum( (A[j+i]-fj*R[i])^2 ) / sum( A[j+i]^2 )  =>
  319. ** vj = ( sum(A[j+i]^2)*sum(R[i]^2) - sum(A[j+i]R[i])^2 ) / sum( A[j+i]^2 )
  320. **
  321. ** In the code variables correspond as follows:
  322. ** cp1        A
  323. ** cp2        R
  324. ** len1        N
  325. ** len2        n
  326. ** aj_m1    A[j-1]
  327. ** aj_lm1    A[j+n-1]
  328. ** sum_ri_2    sum(R[i]^2)
  329. ** sum_aij_2    sum(A[i+j]^2)
  330. ** sum_aij_ri    sum(A[i+j]R[i])
  331. **
  332. ** sum_ri is calculated once, sum_aij_2 is updated each step and sum_aij_ri
  333. ** is completely recalculated each step.
  334. */
  335. static PyObject *
  336. audioop_findfit(self, args)
  337.     PyObject *self;
  338.         PyObject *args;
  339. {
  340.     short *cp1, *cp2;
  341.     int len1, len2;
  342.     int j, best_j;
  343.     double aj_m1, aj_lm1;
  344.     double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
  345.  
  346.     if ( !PyArg_Parse(args, "(s#s#)", &cp1, &len1, &cp2, &len2) )
  347.         return 0;
  348.     if ( len1 & 1 || len2 & 1 ) {
  349.         PyErr_SetString(AudioopError, "Strings should be even-sized");
  350.         return 0;
  351.     }
  352.     len1 >>= 1;
  353.     len2 >>= 1;
  354.     
  355.     if ( len1 < len2 ) {
  356.         PyErr_SetString(AudioopError, "First sample should be longer");
  357.         return 0;
  358.     }
  359.     sum_ri_2 = _sum2(cp2, cp2, len2);
  360.     sum_aij_2 = _sum2(cp1, cp1, len2);
  361.     sum_aij_ri = _sum2(cp1, cp2, len2);
  362.  
  363.     result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2;
  364.  
  365.     best_result = result;
  366.     best_j = 0;
  367.     j = 0;
  368.  
  369.     for ( j=1; j<=len1-len2; j++) {
  370.         aj_m1 = (double)cp1[j-1];
  371.         aj_lm1 = (double)cp1[j+len2-1];
  372.  
  373.         sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1;
  374.         sum_aij_ri = _sum2(cp1+j, cp2, len2);
  375.  
  376.         result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri)
  377.             / sum_aij_2;
  378.  
  379.         if ( result < best_result ) {
  380.             best_result = result;
  381.             best_j = j;
  382.         }
  383.     
  384.     }
  385.  
  386.     factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
  387.     
  388.     return Py_BuildValue("(if)", best_j, factor);
  389. }
  390.  
  391. /*
  392. ** findfactor finds a factor f so that the energy in A-fB is minimal.
  393. ** See the comment for findfit for details.
  394. */
  395. static PyObject *
  396. audioop_findfactor(self, args)
  397.     PyObject *self;
  398.         PyObject *args;
  399. {
  400.     short *cp1, *cp2;
  401.     int len1, len2;
  402.     double sum_ri_2, sum_aij_ri, result;
  403.  
  404.     if ( !PyArg_Parse(args, "(s#s#)", &cp1, &len1, &cp2, &len2) )
  405.         return 0;
  406.     if ( len1 & 1 || len2 & 1 ) {
  407.         PyErr_SetString(AudioopError, "Strings should be even-sized");
  408.         return 0;
  409.     }
  410.     if ( len1 != len2 ) {
  411.         PyErr_SetString(AudioopError, "Samples should be same size");
  412.         return 0;
  413.     }
  414.     len2 >>= 1;
  415.     sum_ri_2 = _sum2(cp2, cp2, len2);
  416.     sum_aij_ri = _sum2(cp1, cp2, len2);
  417.  
  418.     result = sum_aij_ri / sum_ri_2;
  419.  
  420.     return PyFloat_FromDouble(result);
  421. }
  422.  
  423. /*
  424. ** findmax returns the index of the n-sized segment of the input sample
  425. ** that contains the most energy.
  426. */
  427. static PyObject *
  428. audioop_findmax(self, args)
  429.     PyObject *self;
  430.         PyObject *args;
  431. {
  432.     short *cp1;
  433.     int len1, len2;
  434.     int j, best_j;
  435.     double aj_m1, aj_lm1;
  436.     double result, best_result;
  437.  
  438.     if ( !PyArg_Parse(args, "(s#i)", &cp1, &len1, &len2) )
  439.         return 0;
  440.     if ( len1 & 1 ) {
  441.         PyErr_SetString(AudioopError, "Strings should be even-sized");
  442.         return 0;
  443.     }
  444.     len1 >>= 1;
  445.     
  446.     if ( len1 < len2 ) {
  447.         PyErr_SetString(AudioopError, "Input sample should be longer");
  448.         return 0;
  449.     }
  450.  
  451.     result = _sum2(cp1, cp1, len2);
  452.  
  453.     best_result = result;
  454.     best_j = 0;
  455.     j = 0;
  456.  
  457.     for ( j=1; j<=len1-len2; j++) {
  458.         aj_m1 = (double)cp1[j-1];
  459.         aj_lm1 = (double)cp1[j+len2-1];
  460.  
  461.         result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1;
  462.  
  463.         if ( result > best_result ) {
  464.             best_result = result;
  465.             best_j = j;
  466.         }
  467.     
  468.     }
  469.  
  470.     return PyInt_FromLong(best_j);
  471. }
  472.  
  473. static PyObject *
  474. audioop_avgpp(self, args)
  475.     PyObject *self;
  476.         PyObject *args;
  477. {
  478.     signed char *cp;
  479.     int len, size, val = 0, prevval = 0, prevextremevalid = 0,
  480.         prevextreme = 0;
  481.     int i;
  482.     double avg = 0.0;
  483.     int diff, prevdiff, extremediff, nextreme = 0;
  484.  
  485.     if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) )
  486.         return 0;
  487.     if ( size != 1 && size != 2 && size != 4 ) {
  488.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  489.         return 0;
  490.     }
  491.     /* Compute first delta value ahead. Also automatically makes us
  492.     ** skip the first extreme value
  493.     */
  494.     if ( size == 1 )      prevval = (int)*CHARP(cp, 0);
  495.     else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
  496.     else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
  497.     if ( size == 1 )      val = (int)*CHARP(cp, size);
  498.     else if ( size == 2 ) val = (int)*SHORTP(cp, size);
  499.     else if ( size == 4 ) val = (int)*LONGP(cp, size);
  500.     prevdiff = val - prevval;
  501.     
  502.     for ( i=size; i<len; i+= size) {
  503.         if ( size == 1 )      val = (int)*CHARP(cp, i);
  504.         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  505.         else if ( size == 4 ) val = (int)*LONGP(cp, i);
  506.         diff = val - prevval;
  507.         if ( diff*prevdiff < 0 ) {
  508.             /* Derivative changed sign. Compute difference to last
  509.             ** extreme value and remember.
  510.             */
  511.             if ( prevextremevalid ) {
  512.                 extremediff = prevval - prevextreme;
  513.                 if ( extremediff < 0 )
  514.                     extremediff = -extremediff;
  515.                 avg += extremediff;
  516.                 nextreme++;
  517.             }
  518.             prevextremevalid = 1;
  519.             prevextreme = prevval;
  520.         }
  521.         prevval = val;
  522.         if ( diff != 0 )
  523.             prevdiff = diff;    
  524.     }
  525.     if ( nextreme == 0 )
  526.         val = 0;
  527.     else
  528.         val = (int)(avg / (double)nextreme);
  529.     return PyInt_FromLong(val);
  530. }
  531.  
  532. static PyObject *
  533. audioop_maxpp(self, args)
  534.     PyObject *self;
  535.         PyObject *args;
  536. {
  537.     signed char *cp;
  538.     int len, size, val = 0, prevval = 0, prevextremevalid = 0,
  539.         prevextreme = 0;
  540.     int i;
  541.     int max = 0;
  542.     int diff, prevdiff, extremediff;
  543.  
  544.     if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) )
  545.         return 0;
  546.     if ( size != 1 && size != 2 && size != 4 ) {
  547.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  548.         return 0;
  549.     }
  550.     /* Compute first delta value ahead. Also automatically makes us
  551.     ** skip the first extreme value
  552.     */
  553.     if ( size == 1 )      prevval = (int)*CHARP(cp, 0);
  554.     else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
  555.     else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
  556.     if ( size == 1 )      val = (int)*CHARP(cp, size);
  557.     else if ( size == 2 ) val = (int)*SHORTP(cp, size);
  558.     else if ( size == 4 ) val = (int)*LONGP(cp, size);
  559.     prevdiff = val - prevval;
  560.  
  561.     for ( i=size; i<len; i+= size) {
  562.         if ( size == 1 )      val = (int)*CHARP(cp, i);
  563.         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  564.         else if ( size == 4 ) val = (int)*LONGP(cp, i);
  565.         diff = val - prevval;
  566.         if ( diff*prevdiff < 0 ) {
  567.             /* Derivative changed sign. Compute difference to
  568.             ** last extreme value and remember.
  569.             */
  570.             if ( prevextremevalid ) {
  571.                 extremediff = prevval - prevextreme;
  572.                 if ( extremediff < 0 )
  573.                     extremediff = -extremediff;
  574.                 if ( extremediff > max )
  575.                     max = extremediff;
  576.             }
  577.             prevextremevalid = 1;
  578.             prevextreme = prevval;
  579.         }
  580.         prevval = val;
  581.         if ( diff != 0 )
  582.             prevdiff = diff;
  583.     }
  584.     return PyInt_FromLong(max);
  585. }
  586.  
  587. static PyObject *
  588. audioop_cross(self, args)
  589.     PyObject *self;
  590.         PyObject *args;
  591. {
  592.     signed char *cp;
  593.     int len, size, val = 0;
  594.     int i;
  595.     int prevval, ncross;
  596.  
  597.     if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) )
  598.         return 0;
  599.     if ( size != 1 && size != 2 && size != 4 ) {
  600.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  601.         return 0;
  602.     }
  603.     ncross = -1;
  604.     prevval = 17; /* Anything <> 0,1 */
  605.     for ( i=0; i<len; i+= size) {
  606.         if ( size == 1 )      val = ((int)*CHARP(cp, i)) >> 7;
  607.         else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15;
  608.         else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31;
  609.         val = val & 1;
  610.         if ( val != prevval ) ncross++;
  611.         prevval = val;
  612.     }
  613.     return PyInt_FromLong(ncross);
  614. }
  615.  
  616. static PyObject *
  617. audioop_mul(self, args)
  618.     PyObject *self;
  619.         PyObject *args;
  620. {
  621.     signed char *cp, *ncp;
  622.     int len, size, val = 0;
  623.     double factor, fval, maxval;
  624.     PyObject *rv;
  625.     int i;
  626.  
  627.     if ( !PyArg_Parse(args, "(s#id)", &cp, &len, &size, &factor ) )
  628.         return 0;
  629.     
  630.     if ( size == 1 ) maxval = (double) 0x7f;
  631.     else if ( size == 2 ) maxval = (double) 0x7fff;
  632.     else if ( size == 4 ) maxval = (double) 0x7fffffff;
  633.     else {
  634.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  635.         return 0;
  636.     }
  637.     
  638.     rv = PyString_FromStringAndSize(NULL, len);
  639.     if ( rv == 0 )
  640.         return 0;
  641.     ncp = (signed char *)PyString_AsString(rv);
  642.     
  643.     
  644.     for ( i=0; i < len; i += size ) {
  645.         if ( size == 1 )      val = (int)*CHARP(cp, i);
  646.         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  647.         else if ( size == 4 ) val = (int)*LONGP(cp, i);
  648.         fval = (double)val*factor;
  649.         if ( fval > maxval ) fval = maxval;
  650.         else if ( fval < -maxval ) fval = -maxval;
  651.         val = (int)fval;
  652.         if ( size == 1 )      *CHARP(ncp, i) = (signed char)val;
  653.         else if ( size == 2 ) *SHORTP(ncp, i) = (short)val;
  654.         else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val;
  655.     }
  656.     return rv;
  657. }
  658.  
  659. static PyObject *
  660. audioop_tomono(self, args)
  661.     PyObject *self;
  662.         PyObject *args;
  663. {
  664.     signed char *cp, *ncp;
  665.     int len, size, val1 = 0, val2 = 0;
  666.     double fac1, fac2, fval, maxval;
  667.     PyObject *rv;
  668.     int i;
  669.  
  670.     if ( !PyArg_Parse(args, "(s#idd)", &cp, &len, &size, &fac1, &fac2 ) )
  671.         return 0;
  672.     
  673.     if ( size == 1 ) maxval = (double) 0x7f;
  674.     else if ( size == 2 ) maxval = (double) 0x7fff;
  675.     else if ( size == 4 ) maxval = (double) 0x7fffffff;
  676.     else {
  677.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  678.         return 0;
  679.     }
  680.     
  681.     rv = PyString_FromStringAndSize(NULL, len/2);
  682.     if ( rv == 0 )
  683.         return 0;
  684.     ncp = (signed char *)PyString_AsString(rv);
  685.     
  686.     
  687.     for ( i=0; i < len; i += size*2 ) {
  688.         if ( size == 1 )      val1 = (int)*CHARP(cp, i);
  689.         else if ( size == 2 ) val1 = (int)*SHORTP(cp, i);
  690.         else if ( size == 4 ) val1 = (int)*LONGP(cp, i);
  691.         if ( size == 1 )      val2 = (int)*CHARP(cp, i+1);
  692.         else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2);
  693.         else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4);
  694.         fval = (double)val1*fac1 + (double)val2*fac2;
  695.         if ( fval > maxval ) fval = maxval;
  696.         else if ( fval < -maxval ) fval = -maxval;
  697.         val1 = (int)fval;
  698.         if ( size == 1 )      *CHARP(ncp, i/2) = (signed char)val1;
  699.         else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1;
  700.         else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1;
  701.     }
  702.     return rv;
  703. }
  704.  
  705. static PyObject *
  706. audioop_tostereo(self, args)
  707.     PyObject *self;
  708.         PyObject *args;
  709. {
  710.     signed char *cp, *ncp;
  711.     int len, size, val1, val2, val = 0;
  712.     double fac1, fac2, fval, maxval;
  713.     PyObject *rv;
  714.     int i;
  715.  
  716.     if ( !PyArg_Parse(args, "(s#idd)", &cp, &len, &size, &fac1, &fac2 ) )
  717.         return 0;
  718.     
  719.     if ( size == 1 ) maxval = (double) 0x7f;
  720.     else if ( size == 2 ) maxval = (double) 0x7fff;
  721.     else if ( size == 4 ) maxval = (double) 0x7fffffff;
  722.     else {
  723.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  724.         return 0;
  725.     }
  726.     
  727.     rv = PyString_FromStringAndSize(NULL, len*2);
  728.     if ( rv == 0 )
  729.         return 0;
  730.     ncp = (signed char *)PyString_AsString(rv);
  731.     
  732.     
  733.     for ( i=0; i < len; i += size ) {
  734.         if ( size == 1 )      val = (int)*CHARP(cp, i);
  735.         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  736.         else if ( size == 4 ) val = (int)*LONGP(cp, i);
  737.  
  738.         fval = (double)val*fac1;
  739.         if ( fval > maxval ) fval = maxval;
  740.         else if ( fval < -maxval ) fval = -maxval;
  741.         val1 = (int)fval;
  742.  
  743.         fval = (double)val*fac2;
  744.         if ( fval > maxval ) fval = maxval;
  745.         else if ( fval < -maxval ) fval = -maxval;
  746.         val2 = (int)fval;
  747.  
  748.         if ( size == 1 )      *CHARP(ncp, i*2) = (signed char)val1;
  749.         else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
  750.         else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1;
  751.  
  752.         if ( size == 1 )      *CHARP(ncp, i*2+1) = (signed char)val2;
  753.         else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
  754.         else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2;
  755.     }
  756.     return rv;
  757. }
  758.  
  759. static PyObject *
  760. audioop_add(self, args)
  761.     PyObject *self;
  762.         PyObject *args;
  763. {
  764.     signed char *cp1, *cp2, *ncp;
  765.     int len1, len2, size, val1 = 0, val2 = 0, maxval, newval;
  766.     PyObject *rv;
  767.     int i;
  768.  
  769.     if ( !PyArg_Parse(args, "(s#s#i)",
  770.               &cp1, &len1, &cp2, &len2, &size ) )
  771.         return 0;
  772.  
  773.     if ( len1 != len2 ) {
  774.         PyErr_SetString(AudioopError, "Lengths should be the same");
  775.         return 0;
  776.     }
  777.     
  778.     if ( size == 1 ) maxval = 0x7f;
  779.     else if ( size == 2 ) maxval = 0x7fff;
  780.     else if ( size == 4 ) maxval = 0x7fffffff;
  781.     else {
  782.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  783.         return 0;
  784.     }
  785.  
  786.     rv = PyString_FromStringAndSize(NULL, len1);
  787.     if ( rv == 0 )
  788.         return 0;
  789.     ncp = (signed char *)PyString_AsString(rv);
  790.  
  791.     for ( i=0; i < len1; i += size ) {
  792.         if ( size == 1 )      val1 = (int)*CHARP(cp1, i);
  793.         else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
  794.         else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
  795.     
  796.         if ( size == 1 )      val2 = (int)*CHARP(cp2, i);
  797.         else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
  798.         else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);
  799.  
  800.         newval = val1 + val2;
  801.         /* truncate in case of overflow */
  802.         if (newval > maxval) newval = maxval;
  803.         else if (newval < -maxval) newval = -maxval;
  804.         else if (size == 4 && (newval^val1) < 0 && (newval^val2) < 0)
  805.             newval = val1 > 0 ? maxval : - maxval;
  806.  
  807.         if ( size == 1 )      *CHARP(ncp, i) = (signed char)newval;
  808.         else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval;
  809.         else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval;
  810.     }
  811.     return rv;
  812. }
  813.  
  814. static PyObject *
  815. audioop_bias(self, args)
  816.     PyObject *self;
  817.         PyObject *args;
  818. {
  819.     signed char *cp, *ncp;
  820.     int len, size, val = 0;
  821.     PyObject *rv;
  822.     int i;
  823.     int bias;
  824.  
  825.     if ( !PyArg_Parse(args, "(s#ii)",
  826.               &cp, &len, &size , &bias) )
  827.         return 0;
  828.  
  829.     if ( size != 1 && size != 2 && size != 4) {
  830.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  831.         return 0;
  832.     }
  833.     
  834.     rv = PyString_FromStringAndSize(NULL, len);
  835.     if ( rv == 0 )
  836.         return 0;
  837.     ncp = (signed char *)PyString_AsString(rv);
  838.     
  839.     
  840.     for ( i=0; i < len; i += size ) {
  841.         if ( size == 1 )      val = (int)*CHARP(cp, i);
  842.         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  843.         else if ( size == 4 ) val = (int)*LONGP(cp, i);
  844.     
  845.         if ( size == 1 )      *CHARP(ncp, i) = (signed char)(val+bias);
  846.         else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val+bias);
  847.         else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val+bias);
  848.     }
  849.     return rv;
  850. }
  851.  
  852. static PyObject *
  853. audioop_reverse(self, args)
  854.     PyObject *self;
  855.         PyObject *args;
  856. {
  857.     signed char *cp;
  858.     unsigned char *ncp;
  859.     int len, size, val = 0;
  860.     PyObject *rv;
  861.     int i, j;
  862.  
  863.     if ( !PyArg_Parse(args, "(s#i)",
  864.               &cp, &len, &size) )
  865.         return 0;
  866.  
  867.     if ( size != 1 && size != 2 && size != 4 ) {
  868.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  869.         return 0;
  870.     }
  871.     
  872.     rv = PyString_FromStringAndSize(NULL, len);
  873.     if ( rv == 0 )
  874.         return 0;
  875.     ncp = (unsigned char *)PyString_AsString(rv);
  876.     
  877.     for ( i=0; i < len; i += size ) {
  878.         if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
  879.         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  880.         else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
  881.  
  882.         j = len - i - size;
  883.     
  884.         if ( size == 1 )      *CHARP(ncp, j) = (signed char)(val >> 8);
  885.         else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val);
  886.         else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
  887.     }
  888.     return rv;
  889. }
  890.  
  891. static PyObject *
  892. audioop_lin2lin(self, args)
  893.     PyObject *self;
  894.         PyObject *args;
  895. {
  896.     signed char *cp;
  897.     unsigned char *ncp;
  898.     int len, size, size2, val = 0;
  899.     PyObject *rv;
  900.     int i, j;
  901.  
  902.     if ( !PyArg_Parse(args, "(s#ii)",
  903.               &cp, &len, &size, &size2) )
  904.         return 0;
  905.  
  906.     if ( (size != 1 && size != 2 && size != 4) ||
  907.          (size2 != 1 && size2 != 2 && size2 != 4)) {
  908.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  909.         return 0;
  910.     }
  911.     
  912.     rv = PyString_FromStringAndSize(NULL, (len/size)*size2);
  913.     if ( rv == 0 )
  914.         return 0;
  915.     ncp = (unsigned char *)PyString_AsString(rv);
  916.     
  917.     for ( i=0, j=0; i < len; i += size, j += size2 ) {
  918.         if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
  919.         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  920.         else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
  921.  
  922.         if ( size2 == 1 )  *CHARP(ncp, j) = (signed char)(val >> 8);
  923.         else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val);
  924.         else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
  925.     }
  926.     return rv;
  927. }
  928.  
  929. static int
  930. gcd(a, b)
  931.     int a, b;
  932. {
  933.     while (b > 0) {
  934.         int tmp = a % b;
  935.         a = b;
  936.         b = tmp;
  937.     }
  938.     return a;
  939. }
  940.  
  941. static PyObject *
  942. audioop_ratecv(self, args)
  943.     PyObject *self;
  944.     PyObject *args;
  945. {
  946.     char *cp, *ncp;
  947.     int len, size, nchannels, inrate, outrate, weightA, weightB;
  948.     int chan, d, *prev_i, *cur_i, cur_o;
  949.     PyObject *state, *samps, *str, *rv = NULL;
  950.  
  951.     weightA = 1;
  952.     weightB = 0;
  953.     if (!PyArg_ParseTuple(args, "s#iiiiO|ii:ratecv", &cp, &len, &size, &nchannels,
  954.                   &inrate, &outrate, &state, &weightA, &weightB))
  955.         return NULL;
  956.     if (size != 1 && size != 2 && size != 4) {
  957.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  958.         return NULL;
  959.     }
  960.     if (nchannels < 1) {
  961.         PyErr_SetString(AudioopError, "# of channels should be >= 1");
  962.         return NULL;
  963.     }
  964.     if (weightA < 1 || weightB < 0) {
  965.         PyErr_SetString(AudioopError,
  966.             "weightA should be >= 1, weightB should be >= 0");
  967.         return NULL;
  968.     }
  969.     if (len % (size * nchannels) != 0) {
  970.         PyErr_SetString(AudioopError, "not a whole number of frames");
  971.         return NULL;
  972.     }
  973.     if (inrate <= 0 || outrate <= 0) {
  974.         PyErr_SetString(AudioopError, "sampling rate not > 0");
  975.         return NULL;
  976.     }
  977.     /* divide inrate and outrate by their greatest common divisor */
  978.     d = gcd(inrate, outrate);
  979.     inrate /= d;
  980.     outrate /= d;
  981.  
  982.     prev_i = (int *) malloc(nchannels * sizeof(int));
  983.     cur_i = (int *) malloc(nchannels * sizeof(int));
  984.     len /= size * nchannels;    /* # of frames */
  985.     if (prev_i == NULL || cur_i == NULL) {
  986.         (void) PyErr_NoMemory();
  987.         goto exit;
  988.     }
  989.  
  990.     if (state == Py_None) {
  991.         d = -outrate;
  992.         for (chan = 0; chan < nchannels; chan++)
  993.             prev_i[chan] = cur_i[chan] = 0;
  994.     } else {
  995.         if (!PyArg_ParseTuple(state,
  996.                 "iO!;audioop.ratecv: illegal state argument",
  997.                 &d, &PyTuple_Type, &samps))
  998.             goto exit;
  999.         if (PyTuple_Size(samps) != nchannels) {
  1000.             PyErr_SetString(AudioopError,
  1001.                     "illegal state argument");
  1002.             goto exit;
  1003.         }
  1004.         for (chan = 0; chan < nchannels; chan++) {
  1005.             if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan),
  1006.                           "ii:ratecv",&prev_i[chan],&cur_i[chan]))
  1007.                 goto exit;
  1008.         }
  1009.     }
  1010.     str = PyString_FromStringAndSize(
  1011.           NULL, size * nchannels * (len * outrate + inrate - 1) / inrate);
  1012.     if (str == NULL)
  1013.         goto exit;
  1014.     ncp = PyString_AsString(str);
  1015.  
  1016.     for (;;) {
  1017.         while (d < 0) {
  1018.             if (len == 0) {
  1019.                 samps = PyTuple_New(nchannels);
  1020.                 for (chan = 0; chan < nchannels; chan++)
  1021.                     PyTuple_SetItem(samps, chan,
  1022.                         Py_BuildValue("(ii)",
  1023.                                   prev_i[chan],
  1024.                                   cur_i[chan]));
  1025.                 if (PyErr_Occurred())
  1026.                     goto exit;
  1027.                 len = ncp - PyString_AsString(str);
  1028.                 if (len == 0) {
  1029.                     /*don't want to resize to zero length*/
  1030.                     rv = PyString_FromStringAndSize("", 0);
  1031.                     Py_DECREF(str);
  1032.                     str = rv;
  1033.                 } else if (_PyString_Resize(&str, len) < 0)
  1034.                     goto exit;
  1035.                 rv = Py_BuildValue("(O(iO))", str, d, samps);
  1036.                 Py_DECREF(samps);
  1037.                 Py_DECREF(str);
  1038.                 goto exit; /* return rv */
  1039.             }
  1040.             for (chan = 0; chan < nchannels; chan++) {
  1041.                 prev_i[chan] = cur_i[chan];
  1042.                 if (size == 1)
  1043.                     cur_i[chan] = ((int)*CHARP(cp, 0)) << 8;
  1044.                 else if (size == 2)
  1045.                     cur_i[chan] = (int)*SHORTP(cp, 0);
  1046.                 else if (size == 4)
  1047.                     cur_i[chan] = ((int)*LONGP(cp, 0)) >> 16;
  1048.                 cp += size;
  1049.                 /* implements a simple digital filter */
  1050.                 cur_i[chan] =
  1051.                     (weightA * cur_i[chan] +
  1052.                      weightB * prev_i[chan]) /
  1053.                     (weightA + weightB);
  1054.             }
  1055.             len--;
  1056.             d += outrate;
  1057.         }
  1058.         while (d >= 0) {
  1059.             for (chan = 0; chan < nchannels; chan++) {
  1060.                 cur_o = (prev_i[chan] * d +
  1061.                      cur_i[chan] * (outrate - d)) /
  1062.                     outrate;
  1063.                 if (size == 1)
  1064.                     *CHARP(ncp, 0) = (signed char)(cur_o >> 8);
  1065.                 else if (size == 2)
  1066.                     *SHORTP(ncp, 0) = (short)(cur_o);
  1067.                 else if (size == 4)
  1068.                     *LONGP(ncp, 0) = (Py_Int32)(cur_o<<16);
  1069.                 ncp += size;
  1070.             }
  1071.             d -= inrate;
  1072.         }
  1073.     }
  1074.   exit:
  1075.     if (prev_i != NULL)
  1076.         free(prev_i);
  1077.     if (cur_i != NULL)
  1078.         free(cur_i);
  1079.     return rv;
  1080. }
  1081.  
  1082. static PyObject *
  1083. audioop_lin2ulaw(self, args)
  1084.     PyObject *self;
  1085.         PyObject *args;
  1086. {
  1087.     signed char *cp;
  1088.     unsigned char *ncp;
  1089.     int len, size, val = 0;
  1090.     PyObject *rv;
  1091.     int i;
  1092.  
  1093.     if ( !PyArg_Parse(args, "(s#i)",
  1094.               &cp, &len, &size) )
  1095.         return 0;
  1096.  
  1097.     if ( size != 1 && size != 2 && size != 4) {
  1098.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  1099.         return 0;
  1100.     }
  1101.     
  1102.     rv = PyString_FromStringAndSize(NULL, len/size);
  1103.     if ( rv == 0 )
  1104.         return 0;
  1105.     ncp = (unsigned char *)PyString_AsString(rv);
  1106.     
  1107.     for ( i=0; i < len; i += size ) {
  1108.         if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
  1109.         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  1110.         else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
  1111.  
  1112.         *ncp++ = st_linear_to_ulaw(val);
  1113.     }
  1114.     return rv;
  1115. }
  1116.  
  1117. static PyObject *
  1118. audioop_ulaw2lin(self, args)
  1119.     PyObject *self;
  1120.         PyObject *args;
  1121. {
  1122.     unsigned char *cp;
  1123.     unsigned char cval;
  1124.     signed char *ncp;
  1125.     int len, size, val;
  1126.     PyObject *rv;
  1127.     int i;
  1128.  
  1129.     if ( !PyArg_Parse(args, "(s#i)",
  1130.               &cp, &len, &size) )
  1131.         return 0;
  1132.  
  1133.     if ( size != 1 && size != 2 && size != 4) {
  1134.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  1135.         return 0;
  1136.     }
  1137.     
  1138.     rv = PyString_FromStringAndSize(NULL, len*size);
  1139.     if ( rv == 0 )
  1140.         return 0;
  1141.     ncp = (signed char *)PyString_AsString(rv);
  1142.     
  1143.     for ( i=0; i < len*size; i += size ) {
  1144.         cval = *cp++;
  1145.         val = st_ulaw_to_linear(cval);
  1146.     
  1147.         if ( size == 1 )      *CHARP(ncp, i) = (signed char)(val >> 8);
  1148.         else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
  1149.         else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
  1150.     }
  1151.     return rv;
  1152. }
  1153.  
  1154. static PyObject *
  1155. audioop_lin2adpcm(self, args)
  1156.     PyObject *self;
  1157.         PyObject *args;
  1158. {
  1159.     signed char *cp;
  1160.     signed char *ncp;
  1161.     int len, size, val = 0, step, valpred, delta,
  1162.         index, sign, vpdiff, diff;
  1163.     PyObject *rv, *state, *str;
  1164.     int i, outputbuffer = 0, bufferstep;
  1165.  
  1166.     if ( !PyArg_Parse(args, "(s#iO)",
  1167.               &cp, &len, &size, &state) )
  1168.         return 0;
  1169.     
  1170.  
  1171.     if ( size != 1 && size != 2 && size != 4) {
  1172.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  1173.         return 0;
  1174.     }
  1175.     
  1176.     str = PyString_FromStringAndSize(NULL, len/(size*2));
  1177.     if ( str == 0 )
  1178.         return 0;
  1179.     ncp = (signed char *)PyString_AsString(str);
  1180.  
  1181.     /* Decode state, should have (value, step) */
  1182.     if ( state == Py_None ) {
  1183.         /* First time, it seems. Set defaults */
  1184.         valpred = 0;
  1185.         step = 7;
  1186.         index = 0;
  1187.     } else if ( !PyArg_Parse(state, "(ii)", &valpred, &index) )
  1188.         return 0;
  1189.  
  1190.     step = stepsizeTable[index];
  1191.     bufferstep = 1;
  1192.  
  1193.     for ( i=0; i < len; i += size ) {
  1194.         if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
  1195.         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  1196.         else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
  1197.  
  1198.         /* Step 1 - compute difference with previous value */
  1199.         diff = val - valpred;
  1200.         sign = (diff < 0) ? 8 : 0;
  1201.         if ( sign ) diff = (-diff);
  1202.  
  1203.         /* Step 2 - Divide and clamp */
  1204.         /* Note:
  1205.         ** This code *approximately* computes:
  1206.         **    delta = diff*4/step;
  1207.         **    vpdiff = (delta+0.5)*step/4;
  1208.         ** but in shift step bits are dropped. The net result of this
  1209.         ** is that even if you have fast mul/div hardware you cannot
  1210.         ** put it to good use since the fixup would be too expensive.
  1211.         */
  1212.         delta = 0;
  1213.         vpdiff = (step >> 3);
  1214.     
  1215.         if ( diff >= step ) {
  1216.             delta = 4;
  1217.             diff -= step;
  1218.             vpdiff += step;
  1219.         }
  1220.         step >>= 1;
  1221.         if ( diff >= step  ) {
  1222.             delta |= 2;
  1223.             diff -= step;
  1224.             vpdiff += step;
  1225.         }
  1226.         step >>= 1;
  1227.         if ( diff >= step ) {
  1228.             delta |= 1;
  1229.             vpdiff += step;
  1230.         }
  1231.  
  1232.         /* Step 3 - Update previous value */
  1233.         if ( sign )
  1234.             valpred -= vpdiff;
  1235.         else
  1236.             valpred += vpdiff;
  1237.  
  1238.         /* Step 4 - Clamp previous value to 16 bits */
  1239.         if ( valpred > 32767 )
  1240.             valpred = 32767;
  1241.         else if ( valpred < -32768 )
  1242.             valpred = -32768;
  1243.  
  1244.         /* Step 5 - Assemble value, update index and step values */
  1245.         delta |= sign;
  1246.     
  1247.         index += indexTable[delta];
  1248.         if ( index < 0 ) index = 0;
  1249.         if ( index > 88 ) index = 88;
  1250.         step = stepsizeTable[index];
  1251.  
  1252.         /* Step 6 - Output value */
  1253.         if ( bufferstep ) {
  1254.             outputbuffer = (delta << 4) & 0xf0;
  1255.         } else {
  1256.             *ncp++ = (delta & 0x0f) | outputbuffer;
  1257.         }
  1258.         bufferstep = !bufferstep;
  1259.     }
  1260.     rv = Py_BuildValue("(O(ii))", str, valpred, index);
  1261.     Py_DECREF(str);
  1262.     return rv;
  1263. }
  1264.  
  1265. static PyObject *
  1266. audioop_adpcm2lin(self, args)
  1267.     PyObject *self;
  1268.         PyObject *args;
  1269. {
  1270.     signed char *cp;
  1271.     signed char *ncp;
  1272.     int len, size, valpred, step, delta, index, sign, vpdiff;
  1273.     PyObject *rv, *str, *state;
  1274.     int i, inputbuffer = 0, bufferstep;
  1275.  
  1276.     if ( !PyArg_Parse(args, "(s#iO)",
  1277.               &cp, &len, &size, &state) )
  1278.         return 0;
  1279.  
  1280.     if ( size != 1 && size != 2 && size != 4) {
  1281.         PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  1282.         return 0;
  1283.     }
  1284.     
  1285.     /* Decode state, should have (value, step) */
  1286.     if ( state == Py_None ) {
  1287.         /* First time, it seems. Set defaults */
  1288.         valpred = 0;
  1289.         step = 7;
  1290.         index = 0;
  1291.     } else if ( !PyArg_Parse(state, "(ii)", &valpred, &index) )
  1292.         return 0;
  1293.     
  1294.     str = PyString_FromStringAndSize(NULL, len*size*2);
  1295.     if ( str == 0 )
  1296.         return 0;
  1297.     ncp = (signed char *)PyString_AsString(str);
  1298.  
  1299.     step = stepsizeTable[index];
  1300.     bufferstep = 0;
  1301.     
  1302.     for ( i=0; i < len*size*2; i += size ) {
  1303.         /* Step 1 - get the delta value and compute next index */
  1304.         if ( bufferstep ) {
  1305.             delta = inputbuffer & 0xf;
  1306.         } else {
  1307.             inputbuffer = *cp++;
  1308.             delta = (inputbuffer >> 4) & 0xf;
  1309.         }
  1310.  
  1311.         bufferstep = !bufferstep;
  1312.  
  1313.         /* Step 2 - Find new index value (for later) */
  1314.         index += indexTable[delta];
  1315.         if ( index < 0 ) index = 0;
  1316.         if ( index > 88 ) index = 88;
  1317.  
  1318.         /* Step 3 - Separate sign and magnitude */
  1319.         sign = delta & 8;
  1320.         delta = delta & 7;
  1321.  
  1322.         /* Step 4 - Compute difference and new predicted value */
  1323.         /*
  1324.         ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
  1325.         ** in adpcm_coder.
  1326.         */
  1327.         vpdiff = step >> 3;
  1328.         if ( delta & 4 ) vpdiff += step;
  1329.         if ( delta & 2 ) vpdiff += step>>1;
  1330.         if ( delta & 1 ) vpdiff += step>>2;
  1331.  
  1332.         if ( sign )
  1333.             valpred -= vpdiff;
  1334.         else
  1335.             valpred += vpdiff;
  1336.  
  1337.         /* Step 5 - clamp output value */
  1338.         if ( valpred > 32767 )
  1339.             valpred = 32767;
  1340.         else if ( valpred < -32768 )
  1341.             valpred = -32768;
  1342.  
  1343.         /* Step 6 - Update step value */
  1344.         step = stepsizeTable[index];
  1345.  
  1346.         /* Step 6 - Output value */
  1347.         if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8);
  1348.         else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred);
  1349.         else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16);
  1350.     }
  1351.  
  1352.     rv = Py_BuildValue("(O(ii))", str, valpred, index);
  1353.     Py_DECREF(str);
  1354.     return rv;
  1355. }
  1356.  
  1357. static PyMethodDef audioop_methods[] = {
  1358.     { "max", audioop_max },
  1359.     { "minmax", audioop_minmax },
  1360.     { "avg", audioop_avg },
  1361.     { "maxpp", audioop_maxpp },
  1362.     { "avgpp", audioop_avgpp },
  1363.     { "rms", audioop_rms },
  1364.     { "findfit", audioop_findfit },
  1365.     { "findmax", audioop_findmax },
  1366.     { "findfactor", audioop_findfactor },
  1367.     { "cross", audioop_cross },
  1368.     { "mul", audioop_mul },
  1369.     { "add", audioop_add },
  1370.     { "bias", audioop_bias },
  1371.     { "ulaw2lin", audioop_ulaw2lin },
  1372.     { "lin2ulaw", audioop_lin2ulaw },
  1373.     { "lin2lin", audioop_lin2lin },
  1374.     { "adpcm2lin", audioop_adpcm2lin },
  1375.     { "lin2adpcm", audioop_lin2adpcm },
  1376.     { "tomono", audioop_tomono },
  1377.     { "tostereo", audioop_tostereo },
  1378.     { "getsample", audioop_getsample },
  1379.     { "reverse", audioop_reverse },
  1380.     { "ratecv", audioop_ratecv, 1 },
  1381.     { 0,          0 }
  1382. };
  1383.  
  1384. DL_EXPORT(void)
  1385. initaudioop()
  1386. {
  1387.     PyObject *m, *d;
  1388.     m = Py_InitModule("audioop", audioop_methods);
  1389.     d = PyModule_GetDict(m);
  1390.     AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
  1391.     if (AudioopError != NULL)
  1392.          PyDict_SetItemString(d,"error",AudioopError);
  1393. }
  1394.